import { defineStore } from 'pinia'
import { registerActivity, cancelRegistration, getUserRegistrations, getActivityRegistrations, approveRegistration, rejectRegistration } from '@/api/registration'

export const useRegistrationStore = defineStore('registration', {
  state: () => ({
    registrations: [],
    total: 0,
    loading: false,
    isRegistered: false
  }),
  
  actions: {
    // 报名活动
    async registerActivity(registrationData) {
      try {
        const response = await registerActivity(registrationData)
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 取消报名
    async cancelRegistration(registrationId) {
      try {
        const response = await cancelRegistration(registrationId)
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 获取用户报名列表
    async getUserRegistrations(params) {
      this.loading = true
      try {
        const response = await getUserRegistrations(params)
        const { data } = response
        this.registrations = data.records || []
        this.total = data.total || 0
        this.loading = false
        return Promise.resolve(data)
      } catch (error) {
        this.loading = false
        return Promise.reject(error)
      }
    },
    
    // 获取活动报名列表
    async getActivityRegistrations(params) {
      this.loading = true
      try {
        const response = await getActivityRegistrations(params)
        const { data } = response
        this.registrations = data.records || []
        this.total = data.total || 0
        this.loading = false
        return Promise.resolve(data)
      } catch (error) {
        this.loading = false
        return Promise.reject(error)
      }
    },
    
    // 批准报名
    async approveRegistration(registrationId) {
      try {
        const response = await approveRegistration(registrationId)
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 拒绝报名
    async rejectRegistration(registrationId, reason) {
      try {
        const response = await rejectRegistration(registrationId, reason)
        return Promise.resolve(response)
      } catch (error) {
        return Promise.reject(error)
      }
    },
    
    // 检查用户是否已报名
    setRegistrationStatus(status) {
      this.isRegistered = status
    }
  }
}) 